package com.eedi.framework.info.service;

import cn.hutool.core.collection.CollUtil;
import com.eedi.framework.info.controller.org.vo.*;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.exception.util.PreconditionsUtils;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.info.controller.org.vo.*;
import com.eedi.framework.info.convert.OrgInfoConvert;
import com.eedi.framework.info.dal.mapper.OrgInfoMapper;
import com.eedi.framework.info.enums.OrgInfoErrorCodeConstants;
import com.eedi.framework.permission.dal.dataobject.OrgRoleDO;
import com.eedi.framework.permission.dal.dataobject.OrgUserRoleDO;
import com.eedi.framework.permission.dal.mapper.OrgRoleMapper;
import com.eedi.framework.permission.dal.mapper.OrgUserRoleMapper;
import com.eedi.framework.permission.enums.OrgRoleCodeEnum;
import com.eedi.framework.user.controller.org.vo.OrgUserLoginInfoResp;
import com.eedi.framework.user.dal.mapper.OrgUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.framework.info.enums.OrgInfoErrorCodeConstants.ORG_INFO_IS_DISABLE;
import static com.eedi.framework.info.enums.OrgInfoErrorCodeConstants.ORG_INFO_NOT_EXISTS;

@Service
@Slf4j
public class OrgInfoServiceImpl implements OrgInfoService{

    @Resource
    OrgInfoMapper orgInfoMapper;
    @Resource
    OrgUserRoleMapper orgUserRoleMapper;
    @Resource
    OrgRoleMapper orgRoleMapper;
    @Resource
    OrgUserMapper orgUserMapper;

    @Override
    public OrgInfoResp create(OrgInfoCreateReq createReq) {
        //检查该统一信用编码是否已经存在
        OrgInfoResp orgInfoResp = orgInfoMapper.selectByOrgUscCode(createReq.getOrgUscCode());
        PreconditionsUtils.checkArgument(orgInfoResp == null, OrgInfoErrorCodeConstants.ORG_INFO_HAVE_EXISTS, createReq.getOrgUscCode());
        //创建机构基本信息
        return orgInfoMapper.create(createReq);
    }

    @Override
    public int update(OrgInfoUpdateReq updateReqVO) {
        //检查该机构信息是否存在
        OrgInfoResp orgInfoResp = orgInfoMapper.selectByOrgInfoId(updateReqVO.getOrgInfoId());
        PreconditionsUtils.checkArgument(orgInfoResp != null, ORG_INFO_NOT_EXISTS, updateReqVO.getOrgInfoId());
        //更新机构基本信息
        return  orgInfoMapper.update(updateReqVO);

    }

    @Override
    public int disableByOrgInfoId(String orgInfoId) {
        OrgInfoResp orgInfoResp = orgInfoMapper.selectByOrgInfoId(orgInfoId);
        PreconditionsUtils.checkArgument(null!=orgInfoResp, ORG_INFO_NOT_EXISTS,orgInfoId);
        if (CommonStatusEnum.DISABLE.equals(orgInfoResp.getOrgInfoStatus())) {
            throw exception(ORG_INFO_IS_DISABLE);
        }
        return orgInfoMapper.updateStatusByOrgInfoId(orgInfoId, CommonStatusEnum.DISABLE);
    }

    @Override
    public int enableByOrgInfoId(String orgInfoId) {
        OrgInfoResp orgInfoResp = orgInfoMapper.selectByOrgInfoId(orgInfoId);
        PreconditionsUtils.checkArgument(null!=orgInfoResp, ORG_INFO_NOT_EXISTS,orgInfoId);
        if (CommonStatusEnum.ENABLE.equals(orgInfoResp.getOrgInfoStatus())) {
            throw exception(OrgInfoErrorCodeConstants.ORG_INFO_IS_ENABLE);
        }
        return orgInfoMapper.updateStatusByOrgInfoId(orgInfoId, CommonStatusEnum.ENABLE);
    }

    @Override
    public PageResult<OrgInfoResp> page(OrgInfoPageReq req) {
        return orgInfoMapper.page(req);
    }


    @Override
    public List<String> getOrgInfoIdAndTenantIdList() {
//        List<OrgInfoDO> orgInfos = orgInfoMapper.selectList();
//        return orgInfos.stream().map(orgInfo -> orgInfo.getOrgInfoId()+"#"+orgInfo.getSysTenantId()).collect(Collectors.toList());
        return orgInfoMapper.selectAllOrgInfos();
    }

    @Override
    public Boolean validOrg(String orgInfoId) {
        OrgInfoResp orgInfoResp = orgInfoMapper.selectByOrgInfoId(orgInfoId);
        if (orgInfoResp == null) {
            throw exception(ORG_INFO_NOT_EXISTS);
        }
        if (orgInfoResp.getOrgInfoStatus().equals(CommonStatusEnum.DISABLE)) {
            throw exception(ORG_INFO_IS_DISABLE, orgInfoResp.getOrgName());
        }
        return Boolean.TRUE;
    }

    @Override
    public OrgInfoResp getByOrgInfoId(String orgInfoId) {
        return orgInfoMapper.selectByOrgInfoId(orgInfoId);
    }

    @Override
    public List<OrgInfoResp> getByOrgInfoId(Collection<String> orgInfoIds) {
        if (CollUtil.isEmpty(orgInfoIds )){
            return List.of();
        }
        return orgInfoMapper.selectByOrgInfoIds(orgInfoIds);
    }

    @Override
    public List<OrgInfoAdminUserResp> listOrgInfoAdminUsers(String orgInfoId) {
        //查询到机构下的超管角色
        OrgRoleDO orgRole = orgRoleMapper.selectByCodeAndOrgInfoId(orgInfoId,OrgRoleCodeEnum.ORG_ADMIN.getCode(),null);
        //查询超管的角色ID列表
        List<OrgUserRoleDO>  list = this.orgUserRoleMapper.selectListByRoleIds(List.of(orgRole.getOrgRoleId()));
        List<OrgUserLoginInfoResp> resp = this.orgUserMapper.selectByOrgUserIds(list.stream().map(OrgUserRoleDO::getOrgUserId).toList());
        return OrgInfoConvert.INSTANCE.convertListAdminUsers(resp);
    }

    @Override
    public Long countOrgInfos() {
        return this.orgInfoMapper.selectCount();
    }
}
